package com.owen.mapxset;

import com.owen.mapx.MapX;
import com.starloop.i2ctool.I2CUtil;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	public MapX mapx = new MapX(new StarIIC());
	
	void init_mapx() {	
		mapx.init();
		mapx.switchSRC(MapX.SRC_ARM);
	}

	View.OnClickListener on_btn_click = new View.OnClickListener(){
	
		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			int id = view.getId();
			switch(id){
				case R.id.btn_init:
					init_mapx();
					break;
				case R.id.btn_clear:
					mapx.clear();
					break;
				case R.id.btn_mic:
					mapx.setMixer(true);
					mapx.setMixerEnabled(true);
					break;
				case R.id.btn_arm:
					mapx.switchSRC(MapX.SRC_ARM);
					break;
				case R.id.btn_dpl2:
					mapx.setDPL2(true);
					break;
				case R.id.btn_test:
			        break;
				case R.id.btn_darr:
					I2CUtil i2c = new I2CUtil(4, 0x42);
					byte[] data = i2c.read_block(0x3e, 18);
					Log.v("MAPXSET", "size:"+data.length+ " value: " + Integer.toHexString(data[0]) + " " 
					  +Integer.toHexString(data[1]&0xff)+ " "+Integer.toHexString(data[2]));
					break;
			}
		}

	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		int[] btns = {R.id.btn_init, R.id.btn_darr, R.id.btn_mic, R.id.btn_clear, R.id.btn_test,
				R.id.btn_arm, R.id.btn_dpl2};
		for(int i = 0; i<btns.length; i++) {
			Button btn = (Button)findViewById(btns[i]);
			btn.setOnClickListener(on_btn_click);
		}		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
